﻿using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.CallTime.Input;
using HuiLian.Service.Platform.CallTime.Output;
using HuiLian.Service.Platform.CallTime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HuiLian.Repository.Platform.AlarmSchemeSet;
using HuiLian.Service.Platform.AlarmSchemeSet.Output;
using HuiLian.Service.Platform.AlarmSchemeSet.Input;

namespace HuiLian.Service.Platform.AlarmSchemeSet
{
    /// <summary>
    /// 预警方案设置
    /// </summary>
    internal class AlarmSchemeSetService : IAlarmSchemeSetService
    {
        private readonly IMapper _mapper;
        private readonly IAlarmSchemeSetRepository _alarmSchemeSetRepository;

        public AlarmSchemeSetService(IMapper mapper, IAlarmSchemeSetRepository alarmSchemeSetRepository)
        {
            _mapper = mapper;
            _alarmSchemeSetRepository = alarmSchemeSetRepository;
        }

        /// <summary>
        /// 获取预警方案设置
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _alarmSchemeSetRepository.GetAsync(id);
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询预警方案设置
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<AlarmSchemeSetEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _alarmSchemeSetRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(p => new AlarmSchemeSetListOutput { });

            var data = new PageOutput<AlarmSchemeSetListOutput>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加预警方案设置
        /// </summary>
        /// <param name="input">预警方案设置</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(AlarmSchemeSetAddInput input)
        {
            var count = await _alarmSchemeSetRepository.Select.Where(p => p.AlarmSchemeName == input.AlarmSchemeName).CountAsync();
            if (count > 0)
            {
                return ResponseOutput.NotOk("方案名称不能重复");
            }

            var entity = _mapper.Map<AlarmSchemeSetEntity>(input);
            var id = (await _alarmSchemeSetRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新预警方案设置
        /// </summary>
        /// <param name="input">预警方案设置</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(AlarmSchemeSetUpdateInput input)
        {
            var count = await _alarmSchemeSetRepository.Select.Where(p => p.AlarmSchemeName == input.AlarmSchemeName && p.Id != input.Id).CountAsync();
            if (count > 0)
            {
                return ResponseOutput.NotOk("方案名称不能重复");
            }

            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _alarmSchemeSetRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _alarmSchemeSetRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除预警方案设置
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _alarmSchemeSetRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除预警方案设置
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _alarmSchemeSetRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软预警方案设置
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _alarmSchemeSetRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

    }
}
